package codename.carrot.bomberman.game;


import sheep.game.State;
import sheep.graphics.Font;
import sheep.gui.TextButton;
import sheep.gui.WidgetAction;
import sheep.gui.WidgetListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.models.Level;
import codename.carrot.bomberman.models.Player;

/**
 *  This state is active when the game is over, and no more players are alive
 *  it displays the winner, and has a button to go to the main menu
 *  
 *  @author Group A4.
 * 
*/

public class GameOverState extends State implements WidgetListener {
	
	/**
	 *  The button which takes the player to main menu
	 */
    private Level level;
	/**
	 * the player that won the game
	 */
    private Player winner = null;   
	/**
	 * the text to be displayed
	 */
    private String winnerText = null;
	/**
	 * the paint object to paint the {@link GameOverState#winnerText}
	 */
    private Paint winnerPaint = null, 
    /**
     *  the paint object to paint the header text
     */
     headerPaint = null;
	/**
	 * the button that takes the player back to the main menu
	 */
    private TextButton btnMainMenu;
    
    /**
     * Constructor
     *  @param level the level that has been played on
     *  @param winnerID the ID of the player that won the game
     */
    public GameOverState(Level level, int winnerID) {
    	for(Player p: level.getPlayers())
    		if(winnerID == p.getPlayerId())
    			this.winner = p;
    	
    	this.winnerText = "Winner: " + this.winner.getNickname() + "!";
        this.level = level;    
        Font buttonFontIdle = new Font(255,255,255,50,Typeface.SANS_SERIF,Typeface.BOLD);
        Font buttonFontPressed = new Font(255,127,127,50,Typeface.SANS_SERIF,Typeface.BOLD);
        Paint[] buttonPaint = {buttonFontIdle, buttonFontPressed};
        Rect btnMainMenuRect = getTextBoundsRect("Return to main menu", buttonFontIdle);
        int btnMainMenuStartX = AppStorage.SCREEN_WIDTH_GAME / 2 - btnMainMenuRect.width()/2;
        int btnMainMenuStartY = AppStorage.SCREEN_HEIGHT_GAME / 2 + 50 + btnMainMenuRect.height();
        btnMainMenu = new TextButton(btnMainMenuStartX, btnMainMenuStartY, "Return to main menu", buttonPaint);
        addTouchListener(btnMainMenu);
        btnMainMenu.addWidgetListener(this);
    }
    
	/**
	 *  Returns the rect which defines the boundaries (width,height,left,right) of the <code>text</code>
	 * @param text the text to base the returned {@link Rect} on
	 * @param p the paint object to base the returned {@link Rect} on
	 * @return rect the boundaries of the text
	 */
    private Rect getTextBoundsRect(String text, Paint p) {
        Rect rect = new Rect();
        p.getTextBounds(text, 0, text.length(), rect);      
        return rect;
    }
    
    @Override
    public void draw(Canvas canvas) {
    	if(canvas == null)return;
        super.draw(canvas);
        level.draw(canvas);                
        canvas.drawColor(Color.argb(127, 0, 0, 0));
        
        if(headerPaint == null) {
            headerPaint = new Paint();
            headerPaint.setTextSize(60);
            headerPaint.setColor(Color.WHITE);
        }
        
        Rect headerTextRect = getTextBoundsRect("Game Over", headerPaint);
        int headerTextStartX = AppStorage.SCREEN_WIDTH_GAME / 2 - headerTextRect.width()/2;
        int headerTextStartY = 50 + headerTextRect.height()/2;
        canvas.drawText("Game Over", headerTextStartX, headerTextStartY, headerPaint);
               
        if(winner != null) {
            if(winnerPaint == null) {
                winnerPaint = new Paint();
                winnerPaint.setTextSize(40);
                winnerPaint.setColor(Color.WHITE);               
            }
            Rect winnerTextRect = getTextBoundsRect(winnerText, winnerPaint);
            int winnerTextStartX = AppStorage.SCREEN_WIDTH_GAME / 2 - winnerTextRect.width()/2;
            int winnerTextStartY = AppStorage.SCREEN_HEIGHT_GAME / 2 - winnerTextRect.height()/2;
            canvas.drawText(winnerText , winnerTextStartX, winnerTextStartY, winnerPaint);
        }        
        btnMainMenu.draw(canvas);
    }
    
    @Override
    public void actionPerformed(WidgetAction action) {
        if (action.getSource() == btnMainMenu) {
            getGame().popState(); // Pops GameOverState   
            AppStorage.gameActivity.finish();            
        }
    }
}